﻿using apitest.Application.Entities.Pms;

namespace apitest.Application.Test.Services
{
    [DynamicApiController]
    public class Test8Service
    {
        ISqlSugarClient _db;
        public Test8Service(ISqlSugarClient db)
        {
            _db = db;
        }
        public async Task<dynamic> T1(string userName)
        {
            var departList= await _db.Queryable<HR_Department>().ToListAsync();
            var user = await _db.Queryable<HR_User>()
                .FirstAsync(o=>o.name==userName);
            if (user==null)
            {
                throw new Exception("查无此人");
            }
            var res = CheckDepartment(departList, user.deptId);
            return res;
        }
        private string CheckDepartment(List<HR_Department> departmentList,string departId)
        {
            var depart = departmentList.FirstOrDefault(o => o.id == departId);
            if (depart.level=="二级部门")
            {
                return  depart.name;
            }
            else
            {
              return   CheckDepartment(departmentList, depart.parentId);
            }
        }
    }
}
